package com.xyy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xyy.entity.ActivityRegistration;
import com.xyy.vo.ActivityRegistrationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 活动报名Mapper接口
 */
@Mapper
public interface ActivityRegistrationMapper extends BaseMapper<ActivityRegistration> {
    
    /**
     * 根据用户ID获取报名列表（包含活动信息和就诊人信息）
     */
    @Select("SELECT " +
            "ar.id, ar.activity_id, ar.patient_id, ar.status, ar.registration_time, " +
            "ar.confirm_time, ar.cancel_time, ar.remark, ar.created_at, " +
            "a.name as activity_name, a.cover as activity_cover, a.description as activity_description, " +
            "a.location as activity_location, a.start_time as activity_start_time, " +
            "a.end_time as activity_end_time, " +
            "p.name as patient_name, p.gender as patient_gender, p.age as patient_age, " +
            "p.id_card as patient_id_card, p.phone as patient_phone " +
            "FROM activity_registration ar " +
            "LEFT JOIN activity a ON ar.activity_id = a.id " +
            "LEFT JOIN patient p ON ar.patient_id = p.id " +
            "WHERE ar.user_id = #{userId} " +
            "ORDER BY ar.registration_time DESC")
    List<ActivityRegistrationVO> selectRegistrationsByUserId(@Param("userId") Long userId);
    
    /**
     * 根据活动ID获取报名列表（包含用户信息和就诊人信息）
     */
    @Select("SELECT " +
            "ar.id, ar.user_id, ar.patient_id, ar.status, ar.registration_time, " +
            "ar.confirm_time, ar.cancel_time, ar.remark, ar.created_at, " +
            "u.nickname as user_nickname, u.phone as user_phone, " +
            "p.name as patient_name, p.gender as patient_gender, p.age as patient_age, " +
            "p.id_card as patient_id_card, p.phone as patient_phone " +
            "FROM activity_registration ar " +
            "LEFT JOIN user u ON ar.user_id = u.id " +
            "LEFT JOIN patient p ON ar.patient_id = p.id " +
            "WHERE ar.activity_id = #{activityId} " +
            "ORDER BY ar.registration_time ASC")
    List<ActivityRegistrationVO> selectRegistrationsByActivityId(@Param("activityId") Long activityId);
    
    /**
     * 检查用户是否已为指定就诊人报名活动
     */
    @Select("SELECT COUNT(*) FROM activity_registration " +
            "WHERE activity_id = #{activityId} AND patient_id = #{patientId} AND status != 'CANCELLED'")
    int checkRegistrationExists(@Param("activityId") Long activityId, @Param("patientId") Long patientId);
} 